home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / basic / bastips2.zip / BASMAZE.TXT < prev    next >
Text File  |  1986-07-02  |  6KB  |  110 lines

  1.  
  2.                      IBM Mazes and Movement
  3.                   (COMPUTE! Magazine May 1986)
  4.  
  5.      This article discusses how to write a maze game to randomly
  6. generate a simple maze allowing one to choose the dimensions and
  7. still make it solvable.  Few COMPUTE! programs have spawned so many
  8. offspring as Charles Bond's maze generation algorithm originally
  9. published in the December 1981 issue.  This is an adaptation for
  10. SCREEN 1, the medium-resolution graphics screen.  In place of the
  11. PEEKs and POKEs of the earlier versions, this one uses BASIC's
  12. SCREEN, LOCATE, and PRINT statements:
  13.  
  14. 100 KEY OFF:SCREEN 1,0:COLOR 1,0:CLS:RANDOMIZE TIMER
  15. 110 MAXROW=23:MAXCOL=40:DIM P(3,1):FOR J=0 TO 3:READ P(J,0),P(J,1):NEXT
  16. 120 DATA 0,2,-2,0,0,-2,2,0
  17. 130 HL=8:YPOS=2:XPOS=2:LOCATE YPOS,XPOS:PRINT CHR$(5)
  18. 140 J=INT(RND(1)*4):X=J
  19. 150 NY=YPOS+P(J,0):NX=XPOS+P(J,1):IF NY<1 OR NY>MAXROW OR NX<1 OR NX>MAXCOL THEN 170
  20. 160 IF SCREEN(NY,NX)=0 THEN LOCATE NY,NX:PRINT CHR$(J+1):LOCATE (YPOS+P(J,0)/2),(XPOS+P(J,1)/2):PRINT CHR$(HL):YPOS=NY:XPOS=NX:GOTO 140
  21. 170 J=(J+1)*-(J<3):IF J<>X THEN 150
  22. 180 J=SCREEN(YPOS,XPOS)-1:LOCATE YPOS,XPOS:PRINT CHR$(HL);:IF J<4 THEN YPOS=YPOS-P(J,0):XPOS=XPOS-P(J,1):GOTO 150
  23. 190 GOTO 190
  24.  
  25.      To customize the routine for your own use, change MAXROW and
  26. MAXCOL (line 110) for the maximum number of rows and columns in the
  27. maze.  (Don't make MAXROW greater than 23, since printing on the
  28. bottom two lines of the screen causes scrolling.)  As it stands now,
  29. the routine always starts constructing the maze from the upper-left
  30. corner.  You can change this by changing the values of XPOS and YPOS
  31. (line 130).  The values should always be at least 2, but less than
  32. MAXROW and MAXCOL.  The variable HL (line 130) defines the character
  33. used for the paths of the maze.  You can change this to any character
  34. you desire, but its value must be greater than 5 (lower values are
  35. used to draw the maze) and less than 128 (higher values are not
  36. available on the graphics screen).  Unfortunately, this set of
  37. characters does not include a reverse space that would draw solid
  38. paths for the maze.  It's up to you to define which end point is the
  39. finish of the maze.
  40.  
  41.      Now that the maze is in place, it's an ideal time to address
  42. use of the joystick.  The joystick is nice to maneuver a player
  43. through the maze, and BASIC's STICK and STRIG functions make it easy
  44. to read.  IBM joysticks are "positional"; they return values that
  45. reflect the horizontal and vertical deflection of the stick relative
  46. to a simple coordinate system.  In this system, coordinate 0,0 means
  47. the stick is pushed to the upper-left corner, and 255,255 means the
  48. stick is pushed to the lower-right corner.  STICK(0) returns the
  49. horizontal (x) coordinate of the first joystick, while STICK(1)
  50. returns the vertical (y) coordinate.  STICK(2) and STICK(3) perform
  51. the corresponding functions for the second joystick.  The only
  52. special rule is that STICK(0) must be read first, before any other
  53. directions.  (Even if you only want positions from the second
  54. joystick, you must read STICK(0) first.)
  55.  
  56.      STRIG reads the status of the joystick buttons -- most IBM
  57. joysticks have two, but only one per joystick can be read unless
  58. you're using BASICA.  You must use the statement STRIG ON before you
  59. can read button status.  After enabling button reading, STRIG(0)
  60. returns -1 if the primary button on the first joystick has been
  61. pressed since the last time STRIG(0) was called, or 0 if it has not
  62. been pressed.  STRIG(1) is slightly different -- it returns -1 if the
  63. primary button on the first joystick is currently pressed (regardless
  64. of its previous state), or 0 if it is not pressed.  STRIG(2) and
  65. STRIG(3) perform the corresponding functions for the primary button
  66. on the second joystick.
  67.  
  68.      This system makes it easy to determine the position of the
  69. joystick.  But in a situation like navigating the maze drawn by the
  70. routine above, what you really need to know is the direction in which
  71. the stick is pressed.  Add the lines below to the maze-drawing routine:
  72.  
  73. 190 CH=1:XPOS=2:XPOS=2:LOCATE YPOS,XPOS:PRINT CHR$(CH)
  74. 200 XMOV=STICK(0)-XCTR:XJOY=SGN(XMOV):IF ABS(XMOV)<10 THEN XJOY=0
  75. 210 YMOV=STICK(1)-YCTR:YJOY=SGN(YMOV):IF ABS(YMOV)<10 THEN YJOY=0
  76. 220 NY=YPOS+YJOY:NX=XPOS+XJOY:IF NY<1 OR NY>23 OR NX<1 OR NX>40 THEN 200
  77. 230 IF SCREEN(NY,NX)=0 THEN 200
  78. 240 LOCATE YPOS,XPOS:PRINT CHR$(8):LOCATE NY,NX:PRINT CHR$(1):YPOS=NY:XPOS=NX:GOTO 200
  79.  
  80.      Line 190 defines character 1 (the reverse smiling face) as the
  81. player, then positions it at the start of the maze.  Lines 200-210
  82. calculate two directional values, XJOY and YJOY, based on how far the
  83. stick is moved from the center positions (XCTR and YCTR).  XJOY is -1
  84. if the stick is moved to the left, 1 if the stick is moved to the
  85. right, and 0 if the stick is not moved horizontally.  YJOY is -1 if
  86. the stick is moved up, 1 if the stick is moved down, and 0 if the
  87. stick is not moved vertically.
  88.  
  89.      The advantage of this system is that the screen player can be
  90. moved very simply in relationship to the joystick by adding the XJOY
  91. and YJOY values to the current position and using the LOCATE statement
  92. (liens 220 and 240).  The sensitivity of the joystick can be adjusted
  93. by changing the value in the ABS test (lines 200-210).  As shown, the
  94. joystick must be moved at least 10 increments in the desired direction
  95. for the change to register.  This prevents small jiggles of the stick
  96. from causing unwanted movement.  The test in line 230 prevents the
  97. player from leaving the maze.  The SCREEN function returns 0 if no
  98. character has been printed in a position, while a maze path position
  99. will hold the value defined by HL in the maze-drawing routine.
  100.  
  101.      One additional step is required to use this joystick routine.
  102. Each joystick returns slightly different readings, so it's difficult
  103. to predict what the values for the center coordinates will be.  Thus,
  104. it's necessary to calibrate the joystick at the start of every program
  105. that reads it.  The following lines show how this can be done:
  106.  
  107. 10 CLS:WIDTH 40:STRIG ON:PRINT "Press fire button to set center position."
  108. 20 IF STRIG(0)=0 THEN 20
  109. 30 XCTR=STICK(0):YCTR=STICK(1)
  110.